package com.mg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mg.common.Constants;
import com.mg.dto.PurchaseSaleRecordDto;
import com.mg.dto.SimpleResult;
import com.mg.mapper.PurchaseSaleRecordMapper;
import com.mg.model.PurchaseSaleRecord;
import com.mg.service.PurchaseSaleRecordService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by mg1213 on 2018/3/18.
 */
@Service
public class PurchaseSaleRecordServiceImpl implements PurchaseSaleRecordService {
    @Autowired
    private PurchaseSaleRecordMapper purchaseSaleRecordMapper;

    private Logger logger = LogManager.getLogger(PurchaseSaleRecordServiceImpl.class.getName());

    @Override
    public PageInfo<PurchaseSaleRecordDto> selectPageInfo(PurchaseSaleRecordDto purchaseSaleRecordDto, int start, int limit) {
        PageHelper.offsetPage(start, limit);
        List<PurchaseSaleRecordDto> resultList = purchaseSaleRecordMapper.selectPageInfo(purchaseSaleRecordDto);
        PageInfo<PurchaseSaleRecordDto> result = new PageInfo<>(resultList);
        return result;
    }

    @Override
    public SimpleResult insertPurchaseSaleRecord(PurchaseSaleRecord record) {
        SimpleResult result = new SimpleResult();
        try {
            record.setCreateTime(new Date());
            purchaseSaleRecordMapper.insert(record);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("新增出售信息失败 系统未知异常");
            logger.error("新增出售信息失败 系统未知异常", e);
        }
        return result;
    }

    @Override
    public SimpleResult updatePurchaseSaleRecord(PurchaseSaleRecord record) {
        SimpleResult result = new SimpleResult();
        try {
            PurchaseSaleRecord record1 = new PurchaseSaleRecord();
            record1.setId(record.getId());
            record1 = purchaseSaleRecordMapper.selectOne(record1);
            if (record1 == null) {
                result.setSuccess(false);
                result.setMsg("该出售信息不存在，请刷新重试！");
            } else {

                purchaseSaleRecordMapper.updateByPrimaryKeySelective(record);
            }

        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("更新出售信息失败 系统未知异常");
            logger.error("更新出售信息失败 系统未知异常", e);
        }
        return result;
    }

    @Override
    public SimpleResult deletePurchaseSaleRecord(String ids) {
        SimpleResult result = new SimpleResult();
        try {
            String[] idArr = ids.split(",");
            for (String id : idArr) {

                purchaseSaleRecordMapper.deleteByPrimaryKey(Long.parseLong(id));
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("删除出售信息失败 系统未知异常");
            logger.error("删除出售信息失败 系统未知异常", e);
        }
        return result;
    }
}
